#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` [-q quality] in.bam
exit 65
}

while getopts  ":q:p:" opts
do
        case  $opts  in
        q) quality=$OPTARG;;
		p) out_prefix=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

if test -z $quality; then quality=40; fi

echo samtools view quality filter q$quality
samtools view -q$quality -bh $1 > $out_prefix.q$quality.bam

samtools index $out_prefix.q$quality.bam

echo samtools flagstat
samtools flagstat $1 > $out_prefix.flagstat.txt